function Calendar(element, isDoubleCalendar, isPriceShown, sSundayFirstDayOfWeek) {
    this.element = element;
    this.isDoubleCalendar = isDoubleCalendar;
    this.isPriceShown = isPriceShown;
    this.sSundayFirstDayOfWeek = sSundayFirstDayOfWeek;

    // 是否双日历，默认单日历
    if (this.isDoubleCalendar == undefined) {
        this.isDoubleCalendar = false;
    }
    // 是否显示价格，默认不显示
    if (this.isPriceShown == undefined) {
        this.isPriceShown == false;
    }
    // 是否第一天为周日，默认为周一
    if (this.sSundayFirstDayOfWeek == undefined) {
        this.sSundayFirstDayOfWeek == false;
    }

    // 公共变量
    var date = new Date();
    this.year = date.getFullYear();
    this.month = date.getMonth() + 1;

    // 加载
    this.show();
};

Calendar.prototype = {
    constructor: Calendar,
    // 判断闰年
    isLeapYear: function() {
        if ((this.year % 4 == 0) && (this.year % 100 != 0 || this.year % 400 == 0)) {
            days = 29;
        } else {
            days = 28;
        }
    },
    getDays: function(month) {
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                this.isLeapYear(this.year);
                break;
        }
        return days;
    },
    // 获得每月的第一天为周几
    firstDayOfMonth: function(month) {
        var date = new Date();
        date.setFullYear(this.year);
        date.setMonth(month - 1);
        date.setDate(1);
        var firstDay = date.getDay();
        if (!this.sSundayFirstDayOfWeek) {
            if (firstDay != 0) {
                firstDay -= 1;
            } else {
                firstDay = 6;
            }
        }
        return firstDay;
    },
    generateWeek: function() {
        var week = new Array();
        var weekStr = '</div><table><thead>';
        week = ['日', '一', '二', '三', '四', '五', '六', '日'];
        if (this.sSundayFirstDayOfWeek) {
            for (i = 0; i < week.length - 1; i++) {
                weekStr += '<th>' + week[i] + '</th>';
            }
        } else {
            for (i = 1; i < week.length; i++) {
                weekStr += '<th>' + week[i] + '</th>';
            }
        }
        weekStr += '</thead>';
        return weekStr;
    },
    generateDays: function(month) {
        var daysStr = '<tbody>';
        var days = this.getDays(month);
        var notDay = this.firstDayOfMonth(month);

        // 把应当空余的天数创建出来
        for (j = 0; j < notDay; j++) {
            daysStr += '<td></td>';
        }
        // 每7天为一行
        for (i = 1; i <= days; i++) {
            daysStr += '<td><span>' + i + '</span></td>';
            if ((i + notDay) % 7 == 0) {
                daysStr += '</tr><tr>';
            }
        }
        daysStr += "</tbody></table></div>";
        return daysStr;
    },
    generateBtn: function() {
        var btnStr = "<i id='prev' class='prev'></i><i id='next' class='next'></i>";
        return btnStr;
    },
    generateCalendar: function() {
        var calendarStr = '';
        var weekStr = this.generateWeek();
        var titleDateStr = "<div class='calendar' data-date=" + this.year + '-' + this.month + '-' + "><div class='date-title'>" + this.year + '年' + this.month + '月';
        var daysStr = this.generateDays(this.month);
        calendarStr = titleDateStr + weekStr + daysStr;

        if (this.isDoubleCalendar) {
            this.month += 1;
            var titleDateStr = "<div class='calendar' data-date=" + this.year + '-' + this.month + '-' + "><div class='date-title'>" + this.year + '年' + this.month + '月';
            var daysStr = this.generateDays(this.month);
            calendarStr += titleDateStr + weekStr + daysStr;
            this.month -= 1;
        }
        return calendarStr;
    },
    destroy: function() {
        var parent = document.getElementById('calendar');
        parent.removeChild(parent.lastChild);
    },
    renderWrap: function() {
        var el = document.createElement('div');
        document.getElementById(this.element).parentNode.appendChild(el);
        var btnStr = this.generateBtn();
        el.innerHTML = btnStr;
        el.setAttribute('id', 'calendar');
    },
    renderContent: function() {
        var el = document.createElement('div');
        document.getElementById('calendar').appendChild(el);
        el.innerHTML = this.generateCalendar();
        el.setAttribute('class', 'calendar-box');
    },
    monthCheck: function(target) {
        var num = 0;
        if (this.isDoubleCalendar) {
            num = 11; // 双日历，num为11
        } else {
            num = 12; // 但日历，num为12
        };
        if (target == 'prev') {
            // 如果月份大于1，减少月份，否则month为num
            if (this.month > 1) {
                this.month -= 1;
            } else {
                this.year -= 1;
                this.month = num;
            };
        }
        if (target == 'next') {
            // 如果月份小于num，增加月份，否则month为1
            if (this.month < num) {
                this.month += 1;
            } else {
                this.year += 1;
                this.month = 1;
            };
        };
    },
    calendarPosition: function() {
        // var self = document.getElementById('calendar');
        // var height = self.clientHeight;
        // var left = document.getElementById(this.element).offsetLeft;
        // self.style.bottom = '-' + height + 'px';
        // self.style.left = left + 'px';
        if (this.isDoubleCalendar) {
            var parent = document.getElementById('calendar');
            parent.style.width = '570px';
        };
    },
    bindEvent: function() {
        var _this = this;
        // input获得焦点时显示日历
        document.getElementById(this.element).addEventListener('focus', function() {
            document.getElementById('calendar').style.display = 'block';
        }, false);

        // 下月
        document.getElementById('next').addEventListener('click', function() {
            _this.monthCheck(event.target.id);
            _this.destroy();
            _this.renderContent();
        }, false);

        // 上月
        document.getElementById('prev').addEventListener('click', function() {
            _this.monthCheck(event.target.id);
            _this.destroy();
            _this.renderContent();
        }, false);

        // 选中的日期填进input
        document.getElementById('calendar').addEventListener('click', function(event) {
            if (event.target.tagName == 'SPAN') {
                var day = event.target.innerHTML;
                var str = event.target.parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute('data-date');
                var dateStr = str + day;
                document.getElementById(_this.element).value = dateStr;
                this.style.display = 'none';
            }
        }, false);
    },
    show: function() {
        this.renderWrap();
        this.renderContent();
        this.calendarPosition();
        this.bindEvent();
    }
}
